using Sunny.UI;
using System.Collections.Generic;

namespace 人员管理3
{
    public partial class Form1 : UIForm
    {
        internal PersonUtils personUtils;
        private long selId;
        public Form1()
        {
            InitializeComponent();
            personUtils = new PersonUtils();
        }

        /// <summary>
        /// 窗体打开页面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            RefreshList();
        }

        private void RefreshList()
        {
            IEnumerable<Person> enumerable = personUtils.SelectAll();
            dgv1.DataSource = enumerable;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton1_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            //MessageBox.Show($"{name},{age},{sex},{addr}");

            bool b = personUtils.InsertPerson(name, age, sex, addr);
            if (b)
            {
                RefreshList();
                ClearEddit();
            }
        }

        private void ClearEddit()
        {
            tb_name.Text = "";
            tb_age.Text = "";
            cb_sex.Text = "";
            tb_addr.Text = "";
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton2_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            bool b = personUtils.EditPerson(selId, name, age, sex, addr);
            if (b)
            {
                RefreshList();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton3_Click(object sender, EventArgs e)
        {
            bool b = personUtils.DeletePerson(selId);
            if (b)
            {
                RefreshList();
            }
        }

        private void dgv1_SelectionChanged(object sender, EventArgs e)
        {
            int count = dgv1.SelectedRows.Count;
            if (count > 1)
            {
                MessageBox.Show($"你选择了{count}条记录，只能选择一条");
            }
            else if (count == 1)
            {
                DataGridViewRow row = dgv1.SelectedRows[0];
                if (row != null)
                {
                    selId = Convert.ToInt32(row.Cells[0].Value);
                    tb_name.Text = Convert.ToString(row.Cells[0].Value);
                    tb_age.Text = Convert.ToString(row.Cells[1].Value);
                    tb_age.Text = Convert.ToString(row.Cells[2].Value);
                    cb_sex.Text = Convert.ToString(row.Cells[3].Value);
                    tb_addr.Text = Convert.ToString(row.Cells[4].Value);
                }
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton4_Click(object sender, EventArgs e)
        {
            string name = tb_name.Text;
            string age = tb_age.Text;
            string sex = cb_sex.Text;
            string addr = tb_addr.Text;

            IEnumerable<Person> enumerable = personUtils.FindPersons(name, age, sex, addr);
            dgv1.DataSource = enumerable;
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uiButton5_Click(object sender, EventArgs e)
        {
            ClearEddit();
        }
    }
}
